﻿$(document).ready(function(){
	getSource();
});
/**
 * 加载数据
 */

function getSource(){
	$("div[class=holder]").myPagination({
		currPage: 1,
		pageNumber: 15,
		cssStyle:'grayr',
		limit : 10,
		panel:{
			tipInfo_on:true,
			tipInfo_css:{
				width:"22px",height:'12px'
			}
		},
		ajax: {
		  on: true,
		  url: "query",
		  dataType: 'json',
		  param:"ffreezed=1&fdeleted=0&page=1",
		  ajaxStart:function(){
			  ZENG.msgbox.show(" 正在加载中，请稍后...", 6, 10000);
		  },onClick:function(page){
			  $.fn.debug(page);
		  },
		  callback:function(data){
			  ZENG.msgbox.hide(); //隐藏加载提示
			  $.fn.debug(data.source);
			  bindGridList(data);
		  }
		}
	}); 
}
function bindGridList(data){
	$("#total").html('总共找到'+data.total+'条数据');
	var html = "";
	if(data.total > 0){
		$.each(data.source,function(i,template){
			html +='<tr id="web'+i+'" class="tableContent" onclick="ischecked(this)">';// ondblclick="editAjax('+template.id+')"
			html +='<td width="3%" style="padding-left: 5px;"><input type="checkbox" name="web" onchange="checkedInput(this)" onclick="ischecked(document.getElementById(\'web'+i+'\'))" id="td'+ i +'" value="'+template.id+'"/></td>';
			html +='<td width="15%">'+template.fname+'</td>';
			html +='<td width="8%">'+template.fauthor+'</td>';
			html +='<td width="8%">'+(template.fdefinition=="HD"?"高清":"标清")+'</td>';
			html +='<td width="10%">'+(template.fwidth+' × '+template.fheight)+'</td>';
			html +='<td width="10%">'+template.ftype+'</td>';
			html +='<td width="10%">'+(template.flayout == 0?"绝对定位":"流式布局")+'</td>';
			html +='<td>'+template.fdesc+'</td></tr>';
		});
	}else{
		html = "<tr><td colspan='8' align='center'>暂无数据</td></tr>";
	}
	$("#templates").html(html);
}
/**
 * 选中子元素
 * @param obj
 */
function check(obj){
	if(obj.checked){
		var checkbox = $("[name='web']");
		checkbox.attr("checked",'true');//全选 
		$("[class='tableContent']").css("background-color","#D8F79D");
	}else{
		$("[name='web']").removeAttr("checked");//取消全选 
		$("[class='tableContent']").css("background-color","");
	}
}
/**
 * 当前选中项
 * @param obj
 */
function checkedInput(obj){
	var id = obj.id.replace("td","");
	if(obj.checked)
		$("#web"+id).css("background-color","#D8F79D");
	else
		$("#web"+id).css("background-color","");
		
}
/**
 * 单击行时，选中的则取消，未选中的则选中
 * @param obj
 */
function ischecked(obj){
	var id = obj.id;
	var trObj = $("#"+id);
	var tdId = "td"+id.replace("web","");
	var objCheck = $("#"+tdId);
	if(objCheck.attr("checked")){
		objCheck.removeAttr("checked");//取消选中 
		trObj.css("background-color","");
	}else{
		objCheck.attr("checked",'true');//选中
		trObj.css("background-color","#D8F79D");
	}
}
/**
 * 获取所有选中的id
 * @returns {Array}
 */
function checkedInfo(){
	var ids = [];
	$("[name='web'][checked]").each(function(){  
		ids.push($(this).val());  
	}); 
	return ids;  
}
/**
 * 添加信息页面
 */
function addTemplate(){
	//打开新增页面
}

/**
 * 修改
 * @param id
 */
function editAjax(id){
	//传入id进行修改

}
/**
 * 编辑加载数据
 * @param id
 */
function editTemplate(id){
	var ids = checkedInfo();
	if(ids.length == 1){
		editAjax(ids[0]);
	}else if(ids.length == 0){
		var html = "<div style='vertical-align: middle;'><img src='/CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择要修改的信息</div>";	
		var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'editHtmlcloseBtn\').click();"/>';
		Box("editHtml",10,10,html,footerhtml,901);
	}else if(ids.length > 1){
		var html = "<div style='vertical-align: middle;'><img src='/CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;只能修改1条信息！</div>";
		var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'editHtmlcloseBtn\').click();"/>';
		Box("editHtml",10,10,html,footerhtml,901);
	}
}
/**
 * 删除站点信息
 */
function deleteTemplate(){
	var ids = checkedInfo();
	if(ids.length > 0){
		var s = "";
	    for(var i=0;i<ids.length ;i++){
	        s +=ids[i]+",";
	    }
	    if(s != ""){
	    	s = s.substring(0,s.length-1) ;
	    }
		var data = {"ids":s};
		$.ajax({
			url:"deleteTemplate",
			data:data,
			type:"post",
			success:function(data){
				$.growlUI('成功删除数据'+data+'条');
//				getSource();
				$('a[title=1]').click();
			},
			error:function(data){
				$.growlUI('数据删除失败'); 
			}
		
		});
	}else{
		var html = "<div style='vertical-align: middle;'><img src='/CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择要删除的信息</div>";
		var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'deleteHtmlcloseBtn\').click();"/>';
		Box("deleteHtml",10,10,html,footerhtml,901);
	}
}
/**
 * 删除到回收站或冻结数据
 */
function deletedOrfreezed(deleted,freezed){
	var ids = checkedInfo();
	if(ids.length > 0){
		var s = "";
	    for(var i=0;i<ids.length ;i++){
	        s +=ids[i]+",";
	    }
	    if(s != ""){
	    	s = s.substring(0,s.length-1) ;
	    }
		var data = {"ids":s,"fdeleted":deleted,"ffreezed":freezed};
		$.ajax({
			url:"deletedOrfreezed",
			data:data,
			type:"post",
			success:function(data){
				$.growlUI('成功操作数据'+data+'条');
//				getSource();
				$('a[title=1]').click();
			},
			error:function(data){
				$.growlUI('数据操作失败'); 
			}
		
		});
	}else{
		var html = "<div style='vertical-align: middle;'><img src='/CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择要操作的信息</div>";
		var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'deleteHtmlcloseBtn\').click();"/>';
		Box("deleteHtml",10,10,html,footerhtml,901);
	}
}